package del3;
import java.util.*;

class ObservableList<T> implements List<T>{
	private static HashMap<List<?>, ObservableList<?>> listeners = new HashMap<List<?>, ObservableList<?>>();
	
	private List<T> list;
	private List<ObservableListListener> listenerList = new ArrayList<ObservableListListener>();
	
	private ObservableList(List<T> l){
		this.list = l;
	}
	
	public static <K> ObservableList<K> getObservableListInstance(List<K> l){
		if(listeners.containsKey(l))return (ObservableList<K>) listeners.get(l);
		ObservableList<K> ol = new ObservableList<K>(l);
		listeners.put(l, ol);
		return ol;
	}
	
	public void addListener(ObservableListListener oll){
		listenerList.add(oll);
	}
	
	private void notifyListeners(){
		for(ObservableListListener oll : listenerList){
			oll.notifyListChanged();
		}
	}

	public void add(int index, T element) {
		notifyListeners();
		list.add(index, element);
	}


	public boolean add(T e) {
		notifyListeners();
		return list.add(e);
	}


	public boolean addAll(Collection<? extends T> c) {
		notifyListeners();
		return list.addAll(c);
	}


	public boolean addAll(int index, Collection<? extends T> c) {
		notifyListeners();
		return list.addAll(index, c);
	}


	public void clear() {
		notifyListeners();
		list.clear();
	}


	public boolean contains(Object o) {
		return list.contains(o);
	}


	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}


	public boolean equals(Object o) {
		return list.equals(o);
	}


	public T get(int index) {
		return list.get(index);
	}


	public int hashCode() {
		return list.hashCode();
	}


	public int indexOf(Object o) {
		return list.indexOf(o);
	}


	public boolean isEmpty() {
		return list.isEmpty();
	}


	public Iterator<T> iterator() {
		return list.iterator();
	}


	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}


	public ListIterator<T> listIterator() {
		return list.listIterator();
	}


	public ListIterator<T> listIterator(int index) {
		return list.listIterator(index);
	}


	public T remove(int index) {
		notifyListeners();
		return list.remove(index);
	}


	public boolean remove(Object o) {
		notifyListeners();
		return list.remove(o);
	}


	public boolean removeAll(Collection<?> c) {
		notifyListeners();
		return list.removeAll(c);
	}


	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}


	public T set(int index, T element) {
		notifyListeners();
		return list.set(index, element);
	}


	public int size() {
		return list.size();
	}


	public List<T> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}


	public Object[] toArray() {
		return list.toArray();
	}


	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<String> l = new ArrayList<String>();
		ObservableList<String> ola = ObservableList.getObservableListInstance(l);
		
		ola.addListener(new ObservableListListener(){
			public void notifyListChanged(){
				System.out.println("List was changed!");
			}
		});
		
		ola.addListener(new ObservableListListener(){
			public void notifyListChanged(){
				System.out.println("It supports multiple listeners! YAY");
			}
		});
		
		ObservableList<String> olb = ObservableList.getObservableListInstance(l);
		System.out.println("Are they the same? " + (ola == olb ? "YES" : "no"));
		
		ola.add("Hmm this is sad!");
	}
}
